package cn.com.callback


import cn.com.elite.EliteBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.Util;
import net.sf.json.JSONObject;
import cn.com.kh.KHUtil;

/**
 *
 * @author Rick
 * @use 获取用户信息，写入本地
 *
 *
 */
public class GetCustomerList implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(GetCustomerList.class);
	private String charset = "utf-8"; //设置字符集
	private int timeout = 100000; //设置请求超时时间
	private int enableSSLflag = 1;
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		JSONObject result = new JSONObject();
		def smap,oJson,SaveResult;
		try {
			//获取客户端传参
			def r_custname = Util.filterForObject(request.getParameter("customername")); //获取客户姓名
			def r_custmobile = Util.filterForObject(request.getParameter("customermobile")); //获取客户手机
			def r_custidno = Util.filterForObject(request.getParameter("customeridno")); //获取身份证号码
			def r_customerid = Util.filterForObject(request.getParameter("customerid")); //客户编号
			def r_feedwname = Util.filterForObject(request.getParameter("feedwname")); //物业号
			log.debug("NGS传参:"+r_custname+" | "+r_custmobile+" | "+r_custidno+" | "+r_customerid);
			//传参处理

			//列表接口调用
			if(r_custname.equals("")&&r_custmobile.equals("")&&r_custidno.equals("")&&r_customerid.equals("")&&r_feedwname.equals("")) {
				result.put("code", "-1");
				result.put("msg", "未传递参数！");
				return result
			}
			Map<String, String> map = new HashMap<String, String>();
				map.put("customername", r_custname);
				map.put("mobile", r_custmobile);
				map.put("idno", r_custidno);
				map.put("customerid", r_customerid);
				map.put("feedwname", r_feedwname);
				map.put("pagenum", "1");
				map.put("pagesize",	"200");
				map.put("key", "ca3c64a49d1f11ecbed6d703198897725e7aba55");
			def	ASCIIMD5 = KHUtil.ASCIIORDER(map);
			log.debug("接口返回:"+ASCIIMD5);

			oJson = CustHttpPort(eliteBean,ASCIIMD5); //调用接口方法

			log.debug("接口返回:"+oJson);
			return	oJson;


		}catch (Exception e) {
			log.error("errormsg:", e)
			result.put("code", "-1");
			result.put("msg", "调用失败！");
			return result;
		}
	}


/**
 *
 * @use 客户列表接口调用
 * @param eliteBean
 * @param Custname,Mobile,Idno,Custnameid
 * @return
 */
	private String CustHttpPort(EliteBean eliteBean,ASCIIMD5) {
		JSONObject result = new JSONObject();
		def smap,mopsmap,chatqueue,customer_guid,url="";
		try {
			//获取接口地址
			smap = eliteBean.chaxun_sql("select target_url as url from sys_interface_config where type ='getCustomerList'","dataSource");
			if(smap.size()==0) {
				result.put("msg", "找不到对应接口配置");
				result.put("code", "-1");
				return result;
			}else{
				 url = smap[0].getAt("url");
			};



			def postmap=Util.doHttpPost(url,ASCIIMD5.toString(),charset,timeout,enableSSLflag);
			log.debug("接口返回:"+postmap);
			JSONObject obj = JSONObject.fromObject(postmap);
			if(obj.getInt("status")!=200) {
				result.put("code","-2");
				result.put("status",obj.getInt("status"));
				result.put("msg", "接口调用异常");
				return result;
			}else if(obj.get("data").equals("null")||obj.get("data").equals("null")){
				result.put("code","-3");
				result.put("msg", "接口调用无数据");
				return result;
			}
			else{
				return obj;
			};

		}catch (Exception e) {
			log.error("errormsg:", e)
			result.put("code", "-1");
			result.put("msg", "程序异常");
			return result;
		}
	}

}








